#pragma once
#include <string>
#include <jsoncpp/json/json.h>

#include "UserManager.hpp"

class Message{
  public:
    Message(ID_Type id = -1, UserStatus statu = REQUIRE)
    :u_id(id), u_status(statu)
    {}

    void Deserialize(const std::string& buf){
      Json::Reader reader;
      Json::Value val;
      reader.parse(buf, val, false);
      nick_name = val["nick_name"].asString();
      school = val["school"].asString();
      msg = val["message"].asString();
      u_id = val["user_id"].asUInt64();
      u_status = UserStatus(val["u_status"].asInt());
    }

    void Serialize(std::string& buf){
      Json::Value val;
      val["nick_name"] = nick_name;
      val["school"] = school;
      val["message"] = msg;
      val["user_id"] = u_id;
      val["u_status"] = u_status;
      Json::FastWriter writer;
      buf = writer.write(val);
    }

    UserStatus GetStatus(){
      return u_status;
    }

    std::string& GetInfo(UserAttr attr){
      switch(attr){
        case NICKNAME:
          return nick_name;
        case SCHOOL:
          return school;
        case MESSAGE:
          return msg;
        default:
          LOG("%s: GetInfo", level[FATAL]);
      }
    }

    const ID_Type& GetID(){
      return u_id;
    }

    void AlterInfo(const std::string& dest, UserAttr attr){
      switch(attr){
        case NICKNAME:
          nick_name = dest;
        case SCHOOL:
          school = dest;
        case MESSAGE:
          msg = dest;
      }
    }

    void SetStatus(UserStatus statu){
      u_status = statu;
    }

    void SetID(ID_Type id){
      u_id  = id;
    }
  private:
    std::string nick_name;
    std::string school;
    std::string msg;
    ID_Type u_id;
    UserStatus u_status;
};
